package com.itheima.controller.user;


import com.itheima.api.user.UmsResourceCategoryService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.pojo.UmsResourceCategory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {

    @DubboReference
    private UmsResourceCategoryService umsResourceCategoryService;


    /*
    添加后台资源分类
     */
    @PostMapping("/create")
    public AjaxResult create (@RequestBody UmsResourceCategory umsResourceCategory){
        umsResourceCategory.setCreateTime(LocalDateTime.now());
        umsResourceCategoryService.save(umsResourceCategory);
        return AjaxResult.success("操作成功");
    }

    /*
    根据ID删除后台资源
     */
    @PostMapping("/delete/{id}")
    public  AjaxResult delete(@PathVariable Long id){
        umsResourceCategoryService.removeById(id);
        return AjaxResult.success("操作成功");
    }

    /*
    查询所有后台资源分类
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(){
        List<UmsResourceCategory> categoryList = umsResourceCategoryService.list();
        for (UmsResourceCategory category : categoryList) {
            category.setCreateTime(LocalDateTime.now());
        }
        return AjaxResult.success(categoryList);
    }

    /*
修改后台资源分类
     */
@PostMapping("/update/{id}")
    public  AjaxResult update(@PathVariable Long id,@RequestBody UmsResourceCategory umsResourceCategory){
        umsResourceCategory.setId(id);
        umsResourceCategoryService.updateById(umsResourceCategory);
        return AjaxResult.success("操作成功");
    }
}
